package br.com.unifebe.tcc.componente.GUI;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import br.com.unifebe.tcc.DAO.DAOimpl;
import br.com.unifebe.tcc.model.Transacao;

public abstract class DynamicGUI <E extends Object> extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel panelPrincipal;
	private JPanel panelBotoes;

	private Action actionFechar;
	private Action actionCancelar;

	private JCFButton botaoCancelar;
	private JCFButton botaoFechar;
	private JCFButton botaoEditar; 
	private JCFButton botaoGravar;
	private JCFButton botaoNovo;
	private JCFButton botaoExcluir;

	private JToolBar toolBar;
	private Container owner;

	private Transacao transacao;

	public abstract JPanel buildPanel();

	public DynamicGUI() {
		try {
			UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
			//			UIManager.setLookAndFeel(  new NimbusLookAndFeel() );
			//			UIManager.setLookAndFeel( ch.randelshofer.quaqua.QuaquaManager.getLookAndFeel() );
		} catch (Exception e) {
			e.printStackTrace();
		}

		SwingUtilities.updateComponentTreeUI(this);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setCursor(new java.awt.Cursor(Cursor.DEFAULT_CURSOR));
		pack();
		setSize(new Dimension(800,600));
		setContentPane(getPanelPrincipal());
		this.setLocationRelativeTo(null);
	}

	public static JInternalFrame getOwnerForm(Container container) {
		for (Container ownerForm = container; ownerForm != null; ownerForm = ownerForm.getParent() ) {
			if (  ownerForm instanceof JInternalFrame ) {
				return (JInternalFrame)ownerForm;
			}
		}
		return null;
	}

//	public DynamicGUI(Container owner) {
//		this.owner = owner;
//		this.setLayout( new BorderLayout() );
//
//		Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {			
//			@Override
//			public void eventDispatched(AWTEvent event) {
//				Container f =  getOwnerForm( DynamicGUI.this ) ;
//				if (f instanceof JInternalFrame) {
//					JInternalFrame f2 = (JInternalFrame)f;
//					if (f2!=null && !f2.isClosed() && f2.isSelected() ) {
//						if (event instanceof KeyEvent) {
//							KeyEvent keyEvent = (KeyEvent) event;
//							if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
//								int keyCode = keyEvent.getKeyCode();
//								HashMap<Integer, ActionListener> map = null;
//								ActionListener action = map.get( keyCode );								
//								if ( action != null ) {
//									action.actionPerformed( null );
//								}
//							}
//						}
//					}
//				}
//			}
//		}, AWTEvent.KEY_EVENT_MASK);		
//	}

	public DynamicGUI(Component owner, Transacao transacao) {
		super( (Window) owner,  Dialog.ModalityType.APPLICATION_MODAL);
		setTransacao(transacao);
		try {
			UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
			//			UIManager.setLookAndFeel(  new NimbusLookAndFeel() );
			//			UIManager.setLookAndFeel( ch.randelshofer.quaqua.QuaquaManager.getLookAndFeel() );
		} catch (Exception e) {
			e.printStackTrace();
		}

		SwingUtilities.updateComponentTreeUI(this);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setCursor(new java.awt.Cursor(Cursor.DEFAULT_CURSOR));
		pack();
		setSize(new Dimension(800,600));
		setContentPane(getPanelPrincipal());
		this.setLocationRelativeTo(null);
	}

	@SuppressWarnings("static-access")
	public JToolBar getToolBar(){
		if(toolBar == null){
			toolBar = new JToolBar();
			toolBar.setFloatable(false);
			toolBar.setComponentOrientation(getComponentOrientation().RIGHT_TO_LEFT);

			toolBar.add(getBotaoCancelar());
			toolBar.addSeparator(new Dimension(10,1));

			if( Transacao.INCLUIR.equals( getTransacao() )){
				toolBar.add(getBotaoGravar());
				toolBar.addSeparator(new Dimension(10,1));
			}
			if( Transacao.EDITAR.equals( getTransacao() ) ){
				toolBar.add(getBotaoEditar());
				toolBar.addSeparator(new Dimension(10,1));
			}
			if( Transacao.EXCLUIR.equals( getTransacao() ) ){
				toolBar.add(getBotaoExcluir());
			}
		}
		return toolBar;
	}

	public JCFButton getBotaoCancelar(){
		if(botaoCancelar == null){
			botaoCancelar = new JCFButton();
			botaoCancelar.setAction(getActionCancelar());
			botaoCancelar.setText("Cancelar");
			botaoCancelar.setIcon(new ImageIcon(getClass().getResource("/imagens/cancelar2.png")));
		}
		return botaoCancelar;
	}

	public JCFButton getBotaoFechar(){
		if(botaoFechar == null){
			botaoFechar = new JCFButton();
			botaoFechar.setAction(getActionFechar());
			botaoFechar.setText("Fechar");
			botaoFechar.setIcon(new ImageIcon(getClass().getResource("/imagens/cancelar2.png")));
		}
		return botaoFechar;
	}

	public JCFButton getBotaoGravar(){
		if(botaoGravar == null){
			botaoGravar = new JCFButton();
			botaoGravar.setText("Gravar");
			botaoGravar.setIcon(new ImageIcon(getClass().getResource("/imagens/add_verde.png")));
			botaoGravar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if( validar( getObjeto() ) ){
						DAOimpl.getInstance().save(getObjeto());
						setVisible(false);
					}
				}
			});
		}
		return botaoGravar;
	}

	public Boolean validar( E objeto ){
		return true;
	}

	
	public JCFButton getBotaoNovo(){
		if(botaoNovo == null){
			botaoNovo = new JCFButton();
			botaoNovo.setText("Novo");
			botaoNovo.setIcon(new ImageIcon(getClass().getResource("/imagens/add_verde.png")));
		}
		return botaoNovo;
	}
	public JCFButton getBotaoEditar(){
		if(botaoEditar == null){
			botaoEditar = new JCFButton();
			botaoEditar.setText("Editar");
			botaoEditar.setIcon(new ImageIcon(getClass().getResource("/imagens/edit.png")));
			botaoEditar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if( validar( getObjeto() ) ){
						DAOimpl.getInstance().update( getObjeto() );
						dispose();
					}
				}
			});
		}
		return botaoEditar;
	}

	public JCFButton getBotaoExcluir(){
		if(botaoExcluir == null){
			botaoExcluir = new JCFButton();
			botaoExcluir.setText("Excluir");
			botaoExcluir.setIcon(new ImageIcon(getClass().getResource("/imagens/cancelar.png")));
			botaoExcluir.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					DAOimpl.getInstance().remove(getObjeto());
					setVisible(false);
				}
			});
		}
		return botaoExcluir;
	}
	public JPanel getPanelBotoes() {
		if (panelBotoes == null) {
			panelBotoes = new JPanel(new BorderLayout());			
			panelBotoes.add(getToolBar(), BorderLayout.SOUTH);
			panelBotoes.setBorder(BorderFactory.createEtchedBorder());
		}
		return panelBotoes;
	}

	public JPanel getPanelPrincipal() {
		if (panelPrincipal == null) {
			panelPrincipal = new JPanel(new BorderLayout());

			panelPrincipal.add(buildPanel(), BorderLayout.CENTER);
			panelPrincipal.add(getPanelBotoes(), BorderLayout.SOUTH);
		}
		return panelPrincipal;
	}

	@SuppressWarnings("serial")
	public Action getActionFechar(){
		if(actionFechar == null){
			actionFechar = new AbstractAction() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					System.exit(DISPOSE_ON_CLOSE);
				}
			};
		}
		return actionFechar;
	}

	@SuppressWarnings("serial")
	public Action getActionCancelar(){
		if(actionCancelar == null){
			actionCancelar = new AbstractAction() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					setVisible(false);
				}
			};
		}
		return actionCancelar;
	}


	public static final String OWNER_PROPERTY_NAME = "owner";
	public static final String CHILDREN_PROPERTY_NAME = "children";

	public static void setOwner (JFrame child, Component owner){
		Component oldOwner = getOwner(child);
		if (oldOwner != owner){
			child.getRootPane().putClientProperty(OWNER_PROPERTY_NAME, owner);
		}
	}

	/** returns the owner of a JFrame, which was set by setOwner(child, owner)
	 */
	public static Component getOwner (JFrame child){
		return (Component) child.getRootPane().getClientProperty(OWNER_PROPERTY_NAME);
	}

	public Transacao getTransacao(){
		return this.transacao;
	}

	public void setTransacao( Transacao transacao ){
		this.transacao = transacao;
	}

	public abstract E getObjeto();
	public abstract void setObjeto(E e);

}

